package com.quasar.hpatient.view.personal.historyRecord;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.quasar.hpatient.MainAppliaction;
import com.quasar.hpatient.R;
import com.quasar.hpatient.base.BaseActivity;
import com.quasar.hpatient.constant.ConstantManger;
import com.quasar.hpatient.model.custommodel.TemperatureTableItem;
import com.quasar.hpatient.model.httpmodel.AnotherResult;
import com.quasar.hpatient.model.httpmodel.DataStateResult;
import com.quasar.hpatient.model.litePalmodel.DailyItemInfodb;
import com.quasar.hpatient.model.medicalmodel.DailyRecordDetailsData;
import com.quasar.hpatient.onInterface.OnDailyMoreListener;
import com.quasar.hpatient.presenter.DailyMorePresenter;
import com.quasar.hpatient.view.DailyLinActivity_;
import com.quasar.hpatient.view.adapter.HistoryRecordAdapter;
import com.vise.log.ViseLog;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * 历史日常记录
 */
@EActivity(R.layout.activity_history_record_list)
public class HistoryRecordListActivity extends BaseActivity implements OnDailyMoreListener {

    private DailyMorePresenter dailyMorePresenter;
    private HistoryRecordAdapter historyRecordAdapter;
    //日常检查项目信息
    List<DailyItemInfodb> dailyItemInfoList;

    @Extra
    String TimeDate;
    @ViewById(R.id.rv_list)
    RecyclerView rv_list;

    @AfterViews
    void init() {
        initView();
        initData();
    }

    @Override
    protected void initView() {
        initToolbar();

        rv_list.setLayoutManager(new LinearLayoutManager(this));
        rv_list.setHasFixedSize(true);
        rv_list.setNestedScrollingEnabled(false);
    }

    @Override
    protected void initData() {
        dailyItemInfoList = MainAppliaction.getInstance().getDailyItemInfoList();
        dailyMorePresenter = new DailyMorePresenter(this);
        dailyMorePresenter.GetPatientDailySingleRecords(TimeDate);
    }


    //初始化adapter布局
    public void initAdapter(final List<TemperatureTableItem> data) {
        historyRecordAdapter = new HistoryRecordAdapter(data);
        historyRecordAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        rv_list.setAdapter(historyRecordAdapter);
        //item点击
        historyRecordAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

                switch (view.getId()) {
                    case R.id.tv_time:
                        TemperatureTableItem temperatureTableItem=historyRecordAdapter.getData().get(position);

                        if(data.get(position).getLogTime()!=null){
                            String Time=data.get(position).getLogTime();


                            String itemName="";
                            if(temperatureTableItem.getDailyitemName().equals(ConstantManger.BLOODITEMLIST)){
                                itemName=ConstantManger.BLOODPRESSURENAME;
                            }else if(temperatureTableItem.getDailyitemName().equals(ConstantManger.OUTSUM) || temperatureTableItem.getDailyitemName().equals(ConstantManger.INSUM)){
                                itemName=ConstantManger.INOUTPUTVAL;
                            }else{
                                itemName=temperatureTableItem.getDailyitemName();
                            }

                            DailyLinActivity_.intent(HistoryRecordListActivity.this)
                                    .dataTime(Time.substring(0,10)).itemName(itemName).start();
                        }


                        break;
                }
            }
        });
    }


    //返回日常记录查询的数据
    @Override
    public void onDailyInspect(AnotherResult<DailyRecordDetailsData> dataAnotherResult) {

        List<TemperatureTableItem> temperatureTableItemList = new ArrayList<>();
        temperatureTableItemList.add(new TemperatureTableItem(TemperatureTableItem.HEAD));
        for (int i = 0; i < dataAnotherResult.getList().size(); i++) {
            TemperatureTableItem temperatureTableItem = new TemperatureTableItem();
            temperatureTableItem.setDailyItemId(dataAnotherResult.getList().get(i).getDailyItemId());
            temperatureTableItem.setLogTime(dataAnotherResult.getList().get(i).getLogTime());
            temperatureTableItem.setLogValue(dataAnotherResult.getList().get(i).getLogValue());
            temperatureTableItem.setDailyitemDose(dataAnotherResult.getList().get(i).getDailyitemDose());
            temperatureTableItem.setDailyitemName(dataAnotherResult.getList().get(i).getDailyitemName());
            temperatureTableItem.setItemType(TemperatureTableItem.BODY);

            for (int j = 0; j < dailyItemInfoList.size(); j++) {
                if (dailyItemInfoList.get(j).getPrimaryid() == dataAnotherResult.getList().get(i).getDailyItemId()) {
                    temperatureTableItem.setReferenceValue(dailyItemInfoList.get(j).getStandard());//参考值
                    break;
                }
            }

            temperatureTableItemList.add(temperatureTableItem);
        }

        ViseLog.d("获取的数据" + temperatureTableItemList.size());
        initAdapter(temperatureTableItemList);
    }

    @Override
    public void onLineSuccess(AnotherResult<DailyRecordDetailsData> data) {

    }

    @Override
    public void onLineOneDaySuccess(AnotherResult<DailyRecordDetailsData> data) {

    }


    @Override
    public void onSuccessSameday(List<DailyItemInfodb> data) {

    }

    @Override
    public void onError(String msg) {

    }

    @Override
    public void onRefreshUI() {

    }

    @Override
    public void onSubmitSuccess(DataStateResult dataStateResult) {

    }

    @Override
    public void onItemSuccess(DataStateResult dataStateResult) {

    }

    @Override
    public void ondeleteSuccess(DataStateResult dataStateResult) {

    }

    @Override
    public void onLastOneDaySuccess(AnotherResult<DailyRecordDetailsData> dataAnotherResult) {

    }

    @Override
    public void onNextOneDaySuccess(AnotherResult<DailyRecordDetailsData> dataAnotherResult) {

    }
}
